﻿<?php

/*
 * File:        analysis/analysis.php
 * Author:      jack@yeahcpa.com
 * Date:        2011-10-09
 * Intro:       数据分析模块
 */
//引入session操作类
include('../includes/setting/config.inc.php');
include('../includes/class/session.class.php');
include('../includes/class/mysql.class.php');


class YeahTraceAnalysis
{
    private  $content;   //该session中的全部内容
	private  $cachearray;
	private  $cachedatail;
    private  $db;
    function __construct($session_id){

	     //初始化数组;
		 $this->cachearray=array();
		 $this->cachearray['trace_accuser']= $session_id;
		 $this->cachedatail=array();
		 //创建数据库对象
		 $this->db=new   mysql();
		 	
        //读取session id中的全部内容
        $session = new YeahTraceSession($session_id);
        $this->content = $session->ReadAndDel();
		
	
    }
    //实际操作部分
	/*
	 * 数据分析 读取到的数据分为两部分 暂存到数组
	 * first:大体反映用户浏览 $cachearray
	 * second：具体反映用户浏览 $cachedatail
	 */
    function      analysisdata(){
		


	     $this->cachearray['trace_userip']=$this->content['ip'];
	  
         $this->cachearray['trace_useragent']=$this->content['USER_AGENT'];
	   
         unset($this->content['ip']);
		 
         unset($this->content['USER_AGENT']);
		
       $this->cachearray['trace_usercount']=count($this->content); 
	  
	  
	  
            $i=0;
	
		  
       foreach($this->content   as $key  =>  $value)
         {
            if($i==0)
             {
			 
                $this->cachearray['trace_starttime']=$key|0  ;
				
             }
			
          if($i==($this->cachearray['trace_usercount']-1))
            {
			
               $this->cachearray['trace_endtime']=$key|0;
			   
             }
			
              $this->cachedatail[$key]=json_decode($value,true);
			  
              $i++;
         }
      
	  
	  
     }
	
	/*
	 * 数据入库 
	 */
    function     importdata(){
	
	//用户跟踪信息入库
    $this->db->insert($this->cachearray, array("table"=>"user"));
	
	
	
	
	//用户详细数据入库
	
	$sql="";	
    foreach($this->cachedatail   as   $value )
	{
	 $sql.="('".$this->cachearray['trace_accuser']."','".$value['cur_loc']."','".$value['ref_url']."','".$value['data']."','".$this->cachearray['trace_userip']."','".$this->cachearray['trace_useragent']."','".$value['timestamp']."'),";
     }
	 $sql=substr($sql,0,-1);
	 $this->db->batchinsert($sql, array("table"=>"data"));
	 unset($db);
	 unset($this->cachearray);
	 unset($this->cachedatail);
	 unset($this->content);
  }
	
}
?>
